package com.hg.library.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;

/**
 * Created by admin on 2017/2/22.
 */

public class CopyButton extends Button {
    private static final String TAG = "CopyButton";

    private float mPadding = 5;

    private float mRound = 6;
    private float mStroke = 5;

    private final float TRANSLATE = mStroke * 2;

    private float mTranslate = TRANSLATE;

    private boolean isStart;

    public CopyButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        setBackgroundColor(getResources().getColor(android.R.color.transparent));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setAntiAlias(true);

        canvas.drawARGB(0, 255, 255, 255);

        float rectRight = getWidth();
        float rectBottom = getHeight();


        {
            int saveLayerId1 = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);
            canvas.clipRect(0, 0, getWidth() - mStroke * 4 - mPadding, getHeight() - mStroke * 4 - mPadding);

            paint.setColor(Color.argb(255, 65, 72, 75));

            canvas.drawRoundRect(mPadding, mPadding, rectRight - mStroke * 3, rectBottom - mStroke * 3, mRound, mRound, paint);

            paint.clearShadowLayer();
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

            canvas.drawRect(mPadding + mStroke, mPadding + mStroke, rectRight - mStroke * 4, rectBottom - mStroke * 4, paint);
            paint.setXfermode(null);
            canvas.restoreToCount(saveLayerId1);
        }

        {
            int saveLayerId2 = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);
            canvas.translate(mTranslate, mTranslate);

            canvas.clipRect(0, 0, getWidth(), getHeight());

            paint.setColor(Color.argb(255, 65, 72, 75));

            rectRight = getWidth() - mPadding;
            rectBottom = getHeight() - mPadding;
            canvas.drawRoundRect(mPadding, mPadding, rectRight - mStroke * 3, rectBottom - mStroke * 3, mRound, mRound, paint);

            paint.clearShadowLayer();
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

            canvas.drawRect(mPadding + mStroke, mPadding + mStroke, rectRight - mStroke * 4, rectBottom - mStroke * 4, paint);
            paint.setXfermode(null);
            canvas.restoreToCount(saveLayerId2);
        }


        if (isStart) {
            mTranslate += 0.2f;
            if (mTranslate >= TRANSLATE) {
                isStart = false;
                mTranslate = TRANSLATE;
            }
            invalidate();
        }
    }

    public void click() {

        if (!isStart) {
            isStart = true;
            mTranslate = 0;
            invalidate();
        }

    }
}
